package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 套餐控制器
 */
@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetMealController {

    @Resource
    private SetMealService setMealService;

    /**
     * 新建套餐
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    public Result<String> addSetMeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("【新增套餐】setmealDTO:{}", setmealDTO);
        Long tag = setMealService.addSetMeal(setmealDTO);
        if (tag > 0) {
            return Result.success("success");
        } else {
            return Result.error("新增失败");
        }
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    public Result<String> removeByIds(@RequestParam List<Long> ids) {
        log.info("【批量删除套餐】ids:{}",ids);
        Integer tag = setMealService.removeBatchByIds(ids);
        if (tag > 0) {
            return Result.success("删除成功");
        }else  {
            return Result.error("删除失败");
        }
    }


    /**
     * 修改
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    public Result<String> updateById(@RequestBody SetmealDTO setmealDTO) {

        log.info("【修改套餐】dto:{}",setmealDTO);
        Integer tag = setMealService.updateById(setmealDTO);
        if (tag > 0) {
            return Result.success("更改成功");
        }else  {
            return Result.error("更改失败");
        }
    }


    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> findByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("【套餐分页查询】 dto:{}", setmealPageQueryDTO);
        PageResult pageResult = setMealService.findByPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealVO> findById(@PathVariable Long id) {
        log.info("【根据id查询】 id:{}",id);
        SetmealVO setmealVO = setMealService.findById(id);
        return Result.success(setmealVO);
    }


    /**
     * 启用/禁用 套餐
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("【启用/禁用 套餐】 status:{}, id:{}", status, id);
        Integer tag = setMealService.startOrStop(status, id);
        if (tag > 0) {
            return Result.success("状态更新完成");
        } else {
            return Result.error("状态更新失败");
        }
    }

}
